package com.recruit.wechat.controller;

import com.recruit.wechat.controller.entity.ApiCode;
import com.recruit.wechat.controller.helper.$;
import com.recruit.wechat.controller.model.ApiResultModel;
import com.recruit.wechat.utils.FtpUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 肖守凯
 * @packge com.xbxkj.admin.CountryPushController
 * @data 2019-09-02 16:15
 * @project DaTa-Show
 */
@Controller
@RequestMapping("/")
public class IndexController extends BaseController {




    /** 进入系统 登陆页  */
    @RequestMapping("/login.html")
    public String loginHtml(){
        return  "login";
    }
    /** 进入系统 首页  */
    @RequestMapping("/index.html")
    public String indexHtml(Model model){
        model.addAttribute("user",getUser());
       //rabbitTemplate.convertAndSend("report",getUser());

        return  "index";
    }
    /**  首页  iframe 主页*/
    @RequestMapping("/iframe.html")
    public String iframeHtml(){

        return  "iframe";
    }


    // 允许上传的文件类型
    private static final List<String> RESUME_TYPES = new ArrayList<>();

    //允许上传的格式
    static {
//        RESUME_TYPES.add("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
//        RESUME_TYPES.add("application/pdf");
//        RESUME_TYPES.add("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
//        RESUME_TYPES.add("application/msword");
        RESUME_TYPES.add("image/gif");
        RESUME_TYPES.add("image/jpeg");
        RESUME_TYPES.add("image/png");

    }

    // 上传文件大小的上限值
    @Value("${project.resume.max-size}")
    private int resumeMaxSize;


    /**
     * 添加简历到服务器上
     * @param file
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/WeChatUser/setImg")
    @ResponseBody
    public ApiResultModel<String> setImg(MultipartFile file){

        System.out.println("上传了简历");

        String imgUrl = "";

        try {
            if (file.isEmpty()) {
                return $.error("请选择要上传的简历");
            }

            System.out.println("简历大小:" + file.getSize());

            // 检查上传的文件大小
            if (file.getSize() > resumeMaxSize) {

                return $.error("不允许上传超过" + resumeMaxSize / 1024 + "KB的文件");


            }

            // 检查上传的文件类型：image/jpeg, image/png


            if (!RESUME_TYPES.contains(file.getContentType())) {

                return $.error("您上传的头像类型超出了限制，允许上传的文件类型有：" + RESUME_TYPES);

            }


            // 文件先上传至 服务器 返回文件名称
            imgUrl = FtpUtil.uploadFile(file);

            System.out.println("文件名为:" + imgUrl);

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("抛出了异常,删除文件文件后 会报出 空指针异常!");
            return $.error(ApiCode.error,"小伙子你代码写错了");
        }


            return $.success(ApiCode.success,ApiCode.success_msg,"http://"+FtpUtil.FTP_ADDRESS+"/files/LF/"+imgUrl);
    }

}
